package org.myself.meeting.controller;

import org.myself.meeting.Utils.Constant;
import org.myself.meeting.model.Employee;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

//获取登录用户信息并交由websocket管理   提取用户的相关信息并且存储
public class MyWebSocketInterceptor implements HandshakeInterceptor {
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
                                   Map<String, Object> attributes) throws Exception {
        //将ServerHttpRequest转换成request请求相关的类，用来获取request域中的用户信息
        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest servletRequest  = (ServletServerHttpRequest) request;
            HttpServletRequest httpRequest = servletRequest.getServletRequest();
//            //currentuser是request域的key，通过key就可以获取到用户信息 session?
//            Employee user = (Employee)httpRequest.getAttribute("currentuser");
//            //Constants.CURRENT_WEBSOCKET_USER用来存储WebsocketSession的key值
//            attributes.put(Constant.CURRENT_WEBSOCKET_USER,user.getEmployeeid());
        }
        return true;
    }
    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
                               Exception exception) {
    }
}
